/*-----------------------------------------------------
 * File: CU_Run.c
 *
 * Purpose:  Virtual Obj OutDrv 
 *
 *
 * Description:  
 * 
 * Version: 0.01 a0
 *             
 * Copyright 2000 ArSilicii srl
 *-----------------------------------------------------*/
      
#include "hal.h"
#include "ClkGen.h"
#include "capture.h"
#include "CU_Run.h"

struct tCU_Run_Event CU_Run_Event;

enum eCU_Run_State  {Standby,Setup,Run,Alarm } CU_Run_State;

void   CU_Run_Init(void) {
      CU_Run_State=Standby;
}
 
void   CU_Run_Clk(void) {
    static unsigned int tStart;
    
//-- Global jump    
    if(!CU_Run_Event.Start)CU_Run_State=Standby;
//-- 
   
    switch(CU_Run_State) {
       case Standby:
          tStart=ReadWatch();
          CU_Run_Event.Run=0;
          CU_Run_Event.Alarm=0;
          if(CU_Run_Event.Start)CU_Run_State=Setup;       
          break;
          
       case Setup:
          if((ReadWatch()-tStart) >= NVRAM.TStdby){
                CU_Run_State=Run;
          }
          break;

       case Run:
          CU_Run_Event.Run=1;
          if(Capture_Read_Diff()< NVRAM.SogliaH){
                CU_Run_State=Alarm;
                CU_Run_Event.Run=0;
          }          
          
          break;

       case Alarm:
          CU_Run_Event.Alarm=1;
          //LED.DL1=1;
          if(Capture_Read_Diff() > NVRAM.SogliaL){
                CU_Run_State=Run;
                CU_Run_Event.Alarm=0;
                //LED.DL1=0;
          }          
          break;              
    }
}